#!/usr/bin/env python
# -*- coding: utf-8 -*-

import getdata
import sys
import urllib2
try:
        import pygtk
        pygtk.require("2.0")
except:
        pass
try:
        import gtk
        import gtk.glade
except:
        sys.exit(1)

class HellowWorldGTK:
        

        def __init__(self):
                #Set the Glade file
                self.gladefile = "doviz.glade"
                self.wTree = gtk.glade.XML(self.gladefile,"window1") 
                #Create our dictionay and connect it
                dic = { "on_button1_clicked" : self.button1_clicked,
                        "on_window1_destroy" : gtk.main_quit }
                self.wTree.signal_autoconnect(dic)
                self.label1= self.wTree.get_widget("label1")
                self.button1= self.wTree.get_widget("button1")
                self.scrolledwindow1= self.wTree.get_widget("scrolledwindow1")
                self.textview1= self.wTree.get_widget("textview1")
                self.calendar1= self.wTree.get_widget("calendar1")
                self.Result = gtk.TextBuffer()
                
                #self.textbuffer.set_text("bravo")
                self.textview1.set_buffer(self.Result)
                
                self.x=1
                self.button1.set_label("TCMB den döviz bilgisi al ")
                self.label1.set_label("  BUGÜN  ")
                
                




        def button1_clicked(self, widget):
                
                self.button1.set_label("TCMB den bilgiler geliyor")
                dat= self.calendar1.get_date()
                yil=str(dat[0])
                ay=str(dat[1]+1)
                gun=str(dat[2])
                gun1=str(dat[2]-1)
                if len(gun1) < 2 :  gun1="0"+gun1
                if len(gun) < 2 :  gun="0"+gun
                if len(ay) < 2 :  ay="0"+ay
                dat1 = yil+ay+gun
                datnorm = gun+"/"+ay+"/"+yil
                print datnorm

                self.label1.set_label(datnorm)
                urlnorm= "http://www.tcmb.gov.tr/kurlar/"+yil+ay+"/"+gun1+ay+yil+".html"
                print urlnorm

                f = urllib2.urlopen(urlnorm)
                veri=f.read()
                
                veri1=veri[veri.find("EURO"):veri.find("EURO")+50].replace(" ","")
                veri1= veri1.replace("EURO","")
                print veri1[:6]
                print veri1[6:]

                veri2=veri[veri.find("ABD"):veri.find("ABD")+50].replace(" ","")
                veri2= veri2.replace("ABDDOLARI","")
                print veri2[:6]
                print veri2[6:]

                veri3=veri[veri.find("GBP"):veri.find("GBP")+62].replace(" ","")
                #veri3= veri3.replace("TRY1İNGİLİZSTERLİNİ","")
                print veri3[23:29]
                print veri3[29:35]
                
                


                
                bilgi="TCMB        ALıŞ        SATıŞ                 ------------------------------                EUR         "+veri1[:6]+"     "+veri1[6:]
                #self.label1.set_text(bilgi)
                self.Result.set_text(bilgi)
                self.button1.set_label("Lütfen aşağıdaki uyarıyı kontrol edin")
                #getdata.cursor.execute( """select tarih,cinsi,malis,msatis from kur where cinsi='EUR' and tarih =%s  """,(dat1,))
                getdata.cursor.execute( """insert into kur (cinsi,tarih,malis,msatis) values ("EUR",%s,%s,%s) """, (dat1,veri1[:6],veri1[6:]))
                getdata.cursor.execute( """insert into kur (cinsi,tarih,malis,msatis) values ("USD",%s,%s,%s) """, (dat1,veri2[:6],veri2[6:]))
                getdata.cursor.execute( """insert into kur (cinsi,tarih,malis,msatis) values ("GBP",%s,%s,%s) """, (dat1,veri3[23:29],veri3[29:35]))
                self.data = getdata.cursor.fetchall()
                self.fields = getdata.cursor.description

                bilgi= bilgi+"                     *******************************  "+datnorm+"  tarihli kur bilgisi coretex veritabanına başarıyla işlendi. "
                self.Result.set_text(bilgi)

                
                self.x=self.x+1
                #str1=self.fields[0][0]
                #str2=self.data
                #print str1
                #print str2
                #t=str1,str2,"aaa","bbb"




if __name__ == "__main__":
        hwg = HellowWorldGTK()
        gtk.main()

        getdata.cursor.close
        getdata.connection.close
        
        print "bye bye"
